<?php

require("sessFunc.inc.php");
require("dbFunc.inc.php");
require("defs.inc.php");

verifySession();

if(!isset($_SESSION['logged']) || !isset($_SESSION['id']))
{
   header("Location: http://".$_SERVER['HTTP_HOST']
      .dirname($_SERVER['PHP_SELF'])."/error.php?errid=12");
   return;
}

if(!isset($_SESSION['nome']) || strlen($_SESSION['nome']) >= 50)
{
  header("Location: http://".$_SERVER['HTTP_HOST']
      .dirname($_SERVER['PHP_SELF'])."/sell.php?errid=1");
  return;
}

if(!isset($_GET['categoria']))
{
  header("Location: http://".$_SERVER['HTTP_HOST']
      .dirname($_SERVER['PHP_SELF'])."/sell.php?errid=2");
  return;
}

if(!isset($_SESSION['base']) || strval($_SESSION['base']) < 0.01)
{
  header("Location: http://".$_SERVER['HTTP_HOST']
      .dirname($_SERVER['PHP_SELF'])."/sell.php?errid=3");
  return;
}

if(!isset($_SESSION['riserva']) || strval($_SESSION['riserva']) < 0.01)
{
  header("Location: http://".$_SERVER['HTTP_HOST']
      .dirname($_SERVER['PHP_SELF'])."/sell.php?errid=4");
  return;
}

if(!isset($_SESSION['durata']) || strval($_SESSION['durata']) < 3 || strval($_SESSION['durata']) > 60)
{
  header("Location: http://".$_SERVER['HTTP_HOST']
      .dirname($_SERVER['PHP_SELF'])."/sell.php?errid=5");
  return;
}

if(isset($_SESSION['commento']))
{

}

$nome = $_SESSION['nome'];
$categoria = $_GET['categoria'];
$base = $_SESSION['base'];
$riserva = $_SESSION['riserva'];
$durata = $_SESSION['durata'];
$descrizione = $_SESSION['commento'];

$id_oggetto = dbCreateObject($nome, $descrizione, $_SESSION['id'], $categoria);

if(isset($_SESSION['immagine']))
{
  $id_immagine = dbStoreImage($_SESSION['immagine'], $id_oggetto);
  unlink($_SESSION['immagine']);
}

$id_asta = dbCreateAuction($durata, $base, $riserva, $id_oggetto);

$REDIRECT_STRING = "Location: http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/sell.php?ok=true&idau=".$id_asta;



unset($_SESSION['nome']);
unset($_SESSION['base']);
unset($_SESSION['riserva']); 
unset($_SESSION['durata']);
unset($_SESSION['descrizione']);
unset($_SESSION['immagine']);

header($REDIRECT_STRING);
?>